package com.jintian.smart.platform.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jintian.smart.kernel.core.domain.RequestUrlVO;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import com.jintian.smart.base.api.system.constant.SystemSwaggerTagConst;
import com.jintian.smart.base.api.system.form.MenuAddForm;
import com.jintian.smart.base.api.system.form.MenuUpdateForm;
import com.jintian.smart.base.api.system.view.MenuTreeVO;
import com.jintian.smart.base.api.system.view.MenuVO;
import com.jintian.smart.platform.system.service.MenuService;
import com.jintian.smart.kernel.common.context.ContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单
 */
@RestController
@Tag(name = SystemSwaggerTagConst.SYSTEM_MENU)
@RequestMapping("/sys/menu")
public class MenuController {
    @Resource
    private MenuService menuService;

    @Resource
    private List<RequestUrlVO> authUrl;

    @Operation(summary = "添加菜单大")
    @PostMapping("/add")
    @SaCheckPermission("system:menu:add")
    public ResponseDTO<String> addMenu(@RequestBody @Valid MenuAddForm menuAddForm) {
        menuAddForm.setCreateUserId(ContextHolder.getUserIdLong());
        return menuService.addMenu(menuAddForm);
    }

    @Operation(summary = "更新菜单")
    @PostMapping("/update")
    @SaCheckPermission("system:menu:update")
    public ResponseDTO<String> updateMenu(@RequestBody @Valid MenuUpdateForm menuUpdateForm) {
        return menuService.updateMenu(menuUpdateForm);
    }

    @Operation(summary = "批量删除菜单")
    @GetMapping("/batchDelete")
    @SaCheckPermission("system:menu:batchDelete")
    public ResponseDTO<String> batchDeleteMenu(@RequestParam("menuIdList") List<Long> menuIdList) {
        return menuService.batchDeleteMenu(menuIdList, ContextHolder.getUserIdLong());
    }

    @Operation(summary = "查询菜单列表")
    @GetMapping("/query")
    public ResponseDTO<List<MenuVO>> queryMenuList() {
        return ResponseDTO.ok(menuService.queryMenuList(null));
    }

    @Operation(summary = "查询菜单详情")
    @GetMapping("/detail/{menuId}")
    public ResponseDTO<MenuVO> getMenuDetail(@PathVariable Long menuId) {
        return menuService.getMenuDetail(menuId);
    }

    @Operation(summary = "查询菜单树")
    @GetMapping("/tree")
    public ResponseDTO<List<MenuTreeVO>> queryMenuTree(@RequestParam("onlyMenu") Boolean onlyMenu) {
        return menuService.queryMenuTree(onlyMenu);
    }

    @Operation(summary = "获取所有请求路径")
    @GetMapping("/auth/url")
    public ResponseDTO<List<RequestUrlVO>> getAuthUrl() {
        return ResponseDTO.ok(authUrl);
    }
}
